package com.chen.controller;

import com.alibaba.fastjson.JSONObject;
import com.chen.pojo.Article;
import com.chen.pojo.ArticleType;
import com.chen.pojo.Type;
import com.chen.pojo.User;
import com.chen.service.ArticleService;
import com.chen.service.ArticleTypeService;
import com.chen.service.TypeService;
import com.chen.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
@RequestMapping("/article")
public class ArticleController {


    //组合user
    @Autowired
    private UserService userService;
    //组合service
    private final ArticleService articleService;

    //组合类型
    private final TypeService typeService;

    //组合添加类型service
    @Autowired
    private ArticleTypeService articleTypeService;

    @Autowired
    public ArticleController(ArticleService articleService, TypeService typeService) {
        this.articleService = articleService;
        this.typeService = typeService;
    }

    //添加博客
    @RequestMapping(value = "/addBlog",method = POST)
    @ResponseBody
    public String addBlog(Article article, Model model, HttpServletRequest request){

        //获取是否置顶
        //int top_flag = Integer.parseInt(request.getParameter("top_flag"));

        //获取typeId
        int article_typeId = Integer.parseInt(request.getParameter("article_typeId"));


        //创建类型对象
        ArticleType articleType=new ArticleType();

        //加入各项主键
        articleType.setArticle(article);

        //创建type对象
        Type type=new Type();
        type.setType_id(article_typeId);
        articleType.setType(type);

        //把用户添加进去
        User user = userService.getUserByName((String) request.getSession().getServletContext().getAttribute("user"));
        article.setUserId(user);
        //添加博客
        int i = articleService.addBlog(article);
        //添加类别
        articleTypeService.addArt_type(articleType);

        JSONObject json = new JSONObject();
        if (i>0) {

            json.put("msg", "发布博客成功！！");
        }

        return json.toString();
    }

    @RequestMapping("/add")
    public String addPage(Model model){

        //获取所有类型
        List<Type> types = typeService.allType();

        //装入model
        model.addAttribute("types",types);
        //返回添加视图
        return "/admin/addBlog";

    }

    //根据类型查找
    @RequestMapping(value = "/article_type/{pageNum}/{type_id}",method = RequestMethod.GET)
    public String home(@PathVariable("pageNum") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
                       HttpServletResponse response, HttpServletRequest request, @PathVariable("type_id")int type_id
    , Model model) {
        //JSONObject JSON = new JSONObject();
        //博客列表
        List<ArticleType> articles = articleService.getByType(pageNum,pageSize,type_id);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(articles);

        //放入全局变量
        model.addAttribute("articles",articles)
        //最近的
                .addAttribute("recentList",articleService.getRecent())
                .addAttribute("pageInfo",pageInfo)
                .addAttribute("status",2)
                .addAttribute("type_id",type_id);


       return "article/home";
    }

    //根据博客id获取博客详情
    @RequestMapping("/articleDetail/{article_id}")
    public String getArticleDetail(@PathVariable int article_id,Model model){

        Article articleDetail = articleService.getArticleDetail(article_id);

        Type type = typeService.getTypeByArcId(article_id);
        //阅读加1
        articleService.addRead(article_id);

        model.addAttribute("articleDetail",articleDetail)
                .addAttribute("recentList",articleService.getRecent())
                .addAttribute("type",type);

        return "/article/articleDetail";
    }


    //点赞
    @RequestMapping("/praise/{arcId}")
    @ResponseBody
    public String addZan(@PathVariable int arcId){
        JSONObject jsonObject = new JSONObject();
        int i = articleService.addZan(arcId);
        Article articleDetail = articleService.getArticleDetail(arcId);
        jsonObject.put("article",articleDetail);
        return jsonObject.toString();
    }

}
